//
//  NetwookTools.swift
//  mySina
//
//  Created by mac on 15/12/22.
//  Copyright © 2015年 tz. All rights reserved.
//

import AFNetworking

let dataErrorDomain = "com.baidu.data.error"

//枚举任意类型
enum HTTPMethod: String {
    case GET = "GET"
    case POST = "POST"
}

//网络请求的处理层
class NetwookTools: AFHTTPSessionManager {
    //单利对象
    static let sharedTools: NetwookTools = {
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tolls = NetwookTools(baseURL: url)
        tolls.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tolls
    }()
    func requestJSONDict(method:HTTPMethod, urlString:String, parameters:[String : String]?, finished:(dict:[String : AnyObject]?,error: NSError?) -> ()) {
        if method == HTTPMethod.POST {
            //发送POST请求
            POST(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
//                    print(result)
                    guard let dict = result as? [String:AnyObject] else {
                        //不能够转换为字典数据
                        //domain: 反转的域名  com.baidu.error
                        //code : 错误状态码 自定义的错误信息 一般使用 负数
                        let myError = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey:"数据不合法"])
//                        print(myError)
                        //执行失败的回调
                        finished(dict: nil, error: myError)
                        return
                    }
                    //执行成功的回调
                    finished(dict: dict, error: nil)
                }, failure: { (_, error) -> Void in
//                    print(error)
                    //执行失败的回调
                    finished(dict: nil, error: error)
            })
        } else {
            GET(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
//                    print(result)
                    guard let dict = result as? [String:AnyObject] else {
                        let myError = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey:"数据不合法"])
                        //执行失败的回调
                        finished(dict: nil, error: myError)
                        return
                    }
                    //执行成功的回调
                    finished(dict: dict, error: nil)
                }, failure: { (_, error) -> Void in
//                    print(error)
                    //执行失败的回调
                    finished(dict: nil, error: error)
            })
        }
    }
    //上传图片的工具
    func uploadImage(urlString: String,parameters:[String : String]?,imageData: NSData, finished: (dict: [String : AnyObject]?, error: NSError?) -> ()) {
        POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
            //将图片的二进制数据添加formData中
            /**
            *   data 需要上传的文件的二进制数据
            name: 服务器接收上传文件的需要对应字段
            fileName: 服务器存储的名称 随便取  新浪微博 会自己去更改名字
            mimeType: 上传的文件的类型
            */
            formData.appendPartWithFileData(imageData, name: "pic", fileName: "hh", mimeType: "image/jpeg")
            }, progress: { (p) -> Void in
                print(p)
            }, success: { (_, result) -> Void in
                //上传成功
//                print("->\(result)")
                if let dict = result as? [String : AnyObject] {
                    finished(dict: dict, error: nil)
                }
            }) { (_, error) -> Void in
                //上传失败
                finished(dict: nil, error: error)
                print(error)
        }
    }
}